package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.entity.vo.CascaderDataVo;
import com.xueyi.aixihu.entity.vo.DtNewsSortTreeVo;
import com.xueyi.aixihu.entity.vo.DtNewsSortVo;
import com.xueyi.aixihu.entity.vo.SelectVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @authoer:hxs
 * @createDate:2022/8/16
 * @description: 文章、社区分类操作mapper
 */
public interface LocalDtNewsSortMapper {

    /**
     * 通过父级菜单id查询子级菜单
     *
     * @return
     */
    @Select({
            " select id , name  from dt_news_sort where pid = #{id} and is_valid = 1 order by order_number"
    })
    List<SelectVo> findChildMenuByPid(@Param("id") Integer id);


    /**
     * 查询多个父级分类下的子级分类
     *
     * @param pidStr
     * @return
     */
    @Select({
            " select id , name  from dt_news_sort where pid in ( ${pidStr} ) and is_valid = 1 order by order_number"
    })
    List<SelectVo> findChildMenuByPids(@Param("pidStr") String pidStr);

    /**
     * 获取所有的菜单
     *
     * @return
     */
    @Select({
            " select id , name ,pid from dt_news_sort where is_valid = 1"
    })
    List<DtNewsSortVo> findCascaderData();


    /**
     * 查询出所有的父级菜单
     *
     * @return
     */
    @Select(" SELECT id,name label FROM dt_news_sort WHERE pid = 0 and is_valid = 1 ")
    List<DtNewsSortTreeVo> findParentTree();

    /**
     * 根据上级id查询出子级树菜单
     *
     * @return
     */
    @Select(" SELECT id,name label FROM dt_news_sort WHERE pid = #{id} and is_valid = 1 ")
    List<DtNewsSortTreeVo> findChildrenTree(Integer id);

    /**
     * 删除类型管理树的结点
     *
     * @param id
     */
    @Update(" UPDATE dt_news_sort SET is_valid = 0 WHERE id = #{id} ")
    void deleteTreeNode(Integer id);

    /**
     * 增加类型管理树的结点
     *
     * @param pid
     * @param name
     */
    @Insert(" INSERT INTO dt_news_sort (pid,create_time,is_valid,name) " +
            " VALUES (#{pid},NOW(),1,#{name}) ")
    void addTreeNode(Integer pid, String name);
}
